﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Poco;
using Metier;
using WebApp.Models;

namespace WebApp.Controllers
{
    [Authorize(Roles="Manager")]
    public class RestaurantsController : Controller
    {
        //
        // GET: /Restaurants/

        //Affiche la liste des restaurants
        public ActionResult Index()
        {
            GestionRestaurant gestRestaurant = new GestionRestaurant();

            List<Restaurant> restaurants = gestRestaurant.Lister();

            return View(restaurants);
        }

        public ActionResult AjouterRestaurant()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AjouterRestaurant(Restaurant restaurant)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    GestionRestaurant metier = new GestionRestaurant();

                    metier.Ajouter(restaurant);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        //
        // GET: /Manager/DetailRestaurant/5
        public ActionResult Detail(int id)
        {
            DetailRestaurant restoMW = new DetailRestaurant();

            GestionRestaurant resto_manager = new GestionRestaurant();
            GestionZone zone_manager = new GestionZone();

            restoMW.restaurant = resto_manager.Chercher(id);
            restoMW.zones = zone_manager.Lister(restoMW.restaurant);

            return View(restoMW);
        }
        //
        // GET: /Manager/Edit/5
        public ActionResult EditerRestaurant(int id)
        {
            GestionManager gestManager = new GestionManager();
            GestionRestaurant gestResto = new GestionRestaurant();

            ViewBag.managers = gestManager.Lister();

            Restaurant resto = gestResto.Chercher(id);

            return View(resto);
        }
        //
        // POST: /Manager/Edit/5
        [HttpPost]
        public ActionResult EditerRestaurant(Restaurant restaurant)
        {
            try
            {
                GestionRestaurant resto_manager = new GestionRestaurant();
                GestionManager gestManager = new GestionManager();

                if (ModelState.IsValid)
                {
                    //restaurant.Manager = gestManager.Chercher(restaurant.Manager. 

                    resto_manager.MettreAJour(restaurant);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        //
        // GET: /Manager/Delete/5
        public ActionResult SupprimerRestaurant(int id)
        {
            GestionRestaurant resto_manager = new GestionRestaurant();

            resto_manager.Supprimer(resto_manager.Chercher(id));

            return RedirectToAction("Index");
        }
        //
        // POST: /Manager/Delete/5
    }
}
